package com.cyf.base.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Util {

	// md5加密
	public static String md5(String inputText) {
		return encrypt(inputText, "md5");
	}

	// sha加密
	public static String sha(String inputText) {
		return encrypt(inputText, "sha-1");
	}

	/**
	 * md5或者sha-1加密
	 * 
	 * @param inputText
	 *            要加密的内容
	 * @param algorithmName
	 *            加密算法名称：md5或者sha-1，不区分大小写
	 * @return
	 */
	private static String encrypt(String inputText, String algorithmName) {
		if (inputText == null || "".equals(inputText.trim())) {
			throw new IllegalArgumentException("请输入要加密的内容");
		}
		if (algorithmName == null || "".equals(algorithmName.trim())) {
			algorithmName = "md5";
		}
		String encryptText = null;
		try {
			MessageDigest m = MessageDigest.getInstance(algorithmName);
			m.update(inputText.getBytes("UTF8"));
			byte s[] = m.digest();
			return hex(s);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return encryptText;
	}

	// 返回十六进制字符串
	private static String hex(byte[] arr) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < arr.length; ++i) {
			sb.append(Integer.toHexString((arr[i] & 0xFF) | 0x100).substring(1, 3));
		}
		return sb.toString();
	}

	/**
	 * MD5加密并且返回16位大写字母
	 * @param content
	 *            要加密的内容
	 * @return
	 */
	public static String md5AndReturn16(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());
			return getEncode16(digest).toUpperCase();
		} catch (Exception e) {

		}
		return null;
	}

    public static String md516(String content) {
        return md5(content).substring(8, 24);
    }

	/**
	 * MD5加密并且返回32位大写字母
	 * @param content
	 *            要加密的内容
	 * @return
	 */
	public static String md5AndReturn32(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());
			return getEncode32(digest).toUpperCase();
		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 32位加密
	 * @param digest
	 * @return
	 */
	private static String getEncode32(MessageDigest digest) {

		StringBuilder builder = new StringBuilder();
		for (byte b : digest.digest()) {
			builder.append(Integer.toHexString((b >> 4) & 0xf));
			builder.append(Integer.toHexString(b & 0xf));
		}
		//return builder.toString().toLowerCase(); // 小写
		//return builder.toString().toUpperCase(); // 大写
		return builder.toString();
	}

	/**
	 * 16位加密
	 * @param digest
	 * @return
	 */
	private static String getEncode16(MessageDigest digest) {
		StringBuilder builder = new StringBuilder();
		for (byte b : digest.digest()) {
			builder.append(Integer.toHexString((b >> 4) & 0xf));
			builder.append(Integer.toHexString(b & 0xf));
		}
		// 16位加密，从第9位到25位
		return builder.substring(8, 24);
	}

	/**
	 * MD5加密并且返回16位大写字母
	 * @param content
	 *            要加密的内容
	 * @return
	 */
	public static String md5AndReturn16ThatUpperCase(String content) {
		try {
			MessageDigest digest = MessageDigest.getInstance("MD5");
			digest.update(content.getBytes());
			return getEncode16(digest).toUpperCase();
		} catch (Exception e) {

		}
		return null;
	}


	public static void main(String[] args) throws Exception {
//		System.out.println(md5(new File("d:\\183.apk")));
		System.out.println(MD5Util.md5("78823909805293568" + ""));
	}
	

}
